package com.zhongshengnetwork.rightbe.game.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dd.CircularProgressButton;
import com.tencent.connect.common.Constants;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.wang.avi.AVLoadingIndicatorView;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BroadcastDefine;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.customview.ActionSheetDialog;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.customview.PublishSelectPicPopupWindow;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.GuessWordAnswerModel;
import com.zhongshengnetwork.rightbe.gsonmodel.HomeDataModel;
import com.zhongshengnetwork.rightbe.gsonmodel.LogicalSubjectModel;
import com.zhongshengnetwork.rightbe.gsonmodel.LoginModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WordUserinfoModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.login.LoginActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LogicalSubjectActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{
    private final static String url = "http://a.app.qq.com/o/simple.jsp?pkgname=com.zhongshengnetwork.rightbe";//Constant.HOST+"downapp.do?";
    private List<String> list;
    private PublishSelectPicPopupWindow menuWindow;
    private HomeDataModel homeDataModel;
    private TopBarView topbar;

    private TextView logical_subject_tip;
    private CircularProgressButton logical_answer_upload;
    private CircularProgressButton logical_subject_jiexi;
    private CircularProgressButton logical_subject_help;
    private CircularProgressButton logical_subject_jinbi;

    private TextView logical_subject_title;
    private TextView logical_subject_content;
    private RelativeLayout logical_subject_bg;
    private ImageView logical_subject_img;

    private TextView logical_subject_optiona;
    private TextView logical_subject_optionb;
    private TextView logical_subject_optionc;
    private TextView logical_subject_optiond;

    private RelativeLayout logical_subject_layout;

    private String selectAnswer = "";
    private WordUserinfoModel wordUserinfoModel = null;
    private LogicalSubjectModel logicalSubjectModel = null;
    private boolean isGetTip = false;
    private boolean isChecking = false;
    private int goldcount = 0;
    private AVLoadingIndicatorView avi;

    private MyIUiListener mIUiListener = new MyIUiListener();
    class MyIUiListener implements IUiListener {
        @Override
        public void onComplete(Object o) {
            Log.e("TAG","分享成功");
            getShareGold();
        }

        @Override
        public void onError(UiError uiError) {
            Log.e("TAG","分享出错");
        }

        @Override
        public void onCancel() {
            Log.e("TAG","分享取消");
        }

        @Override
        public void onWarning(int i) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, mIUiListener);
        if (requestCode == Constants.REQUEST_API) {
            if (resultCode == Constants.REQUEST_QQ_SHARE || resultCode == Constants.REQUEST_QZONE_SHARE || resultCode == Constants.REQUEST_OLD_SHARE) {
                Tencent.handleResultData(data, mIUiListener);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logical_subject);
        homeDataModel = (HomeDataModel)getIntent().getSerializableExtra("model");
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        topbar.getTitleView().setText(homeDataModel.getName());
        topbar.getRightTextView().setText("排行榜");
        topbar.getRightTextView().setTextSize(15);
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken()) || CustomApplication.isAutoLogin == true){
                    startActivity(new Intent(LogicalSubjectActivity.this, LoginActivity.class));
                    finish();
                    return;
                }
                if (homeDataModel != null){
                    Intent intent = new Intent(LogicalSubjectActivity.this,GameRangeActivity.class);
                    intent.putExtra("appid",homeDataModel.getAppid());
                    startActivity(intent);
                }

            }
        });
        initUI();
        list = new ArrayList<String>();
        list.add("分享给微信好友");
        list.add("分享到微信朋友圈");
        list.add("分享给QQ好友");
        list.add("分享到QQ空间");
        //list.add("取消");
        getData();
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    private void initUI()
    {
        logical_subject_layout = (RelativeLayout) findViewById(R.id.logical_subject_layout);
        logical_subject_tip = (TextView) findViewById(R.id.logical_subject_tip);
        logical_answer_upload = (CircularProgressButton) findViewById(R.id.logical_answer_upload);
        logical_answer_upload.setBackgroundColor(getResources().getColor(R.color.mainColor));
        logical_answer_upload.setStrokeColor(getResources().getColor(R.color.mainColor));
        logical_answer_upload.setText("提交答案");

        logical_subject_jiexi = (CircularProgressButton) findViewById(R.id.logical_subject_jiexi);
        logical_subject_jiexi.setBackgroundColor(getResources().getColor(R.color.wordBgColor));
        logical_subject_jiexi.setStrokeColor(getResources().getColor(R.color.wordBgColor));
        logical_subject_jiexi.setText("查看解析");
        logical_subject_help = (CircularProgressButton) findViewById(R.id.logical_subject_help);
        logical_subject_help.setBackgroundColor(getResources().getColor(R.color.limitBackgroundColor));
        logical_subject_help.setStrokeColor(getResources().getColor(R.color.limitBackgroundColor));
        logical_subject_help.setText("求助分享");
        logical_subject_jinbi = (CircularProgressButton) findViewById(R.id.logical_subject_jinbi);
        logical_subject_jinbi.setBackgroundColor(getResources().getColor(R.color.orangeColor));
        logical_subject_jinbi.setStrokeColor(getResources().getColor(R.color.orangeColor));
        logical_subject_jinbi.setText("领取金币");

        logical_subject_title = (TextView) findViewById(R.id.logical_subject_title);
        logical_subject_content = (TextView) findViewById(R.id.logical_subject_content);
        logical_subject_bg = (RelativeLayout) findViewById(R.id.logical_subject_bg);
        logical_subject_bg = (RelativeLayout) findViewById(R.id.logical_subject_bg);
        logical_subject_img = (ImageView) findViewById(R.id.logical_subject_img);

        logical_subject_optiona = (TextView) findViewById(R.id.logical_subject_optiona);
        logical_subject_optionb = (TextView) findViewById(R.id.logical_subject_optionb);
        logical_subject_optionc = (TextView) findViewById(R.id.logical_subject_optionc);
        logical_subject_optiond = (TextView) findViewById(R.id.logical_subject_optiond);

        logical_subject_optiona.setPadding(25,0,0,0);
        logical_subject_optionb.setPadding(25,0,0,0);
        logical_subject_optionc.setPadding(25,0,0,0);
        logical_subject_optiond.setPadding(25,0,0,0);

        avi = (AVLoadingIndicatorView)findViewById(R.id.avi);
    }

    public void onClickUploadAnswer(View view){
        Log.e("TAG","点击提交答案");
        checkAnswer();
    }

    public void onClickMenu(View view){
        switch (view.getId()){
            case R.id.logical_subject_jiexi:
                getExplain();
                break;
            case R.id.logical_subject_help:
                share(view);
                break;
            case R.id.logical_subject_jinbi:
                signDo();
                break;
        }
    }

    public void changeToNormal(){
        logical_subject_optiona.setBackground(getResources().getDrawable(R.drawable.whitestyle));
        logical_subject_optiona.setTextColor(Color.parseColor("#434A54"));
        logical_subject_optionb.setBackground(getResources().getDrawable(R.drawable.whitestyle));
        logical_subject_optionb.setTextColor(Color.parseColor("#434A54"));
        logical_subject_optionc.setBackground(getResources().getDrawable(R.drawable.whitestyle));
        logical_subject_optionc.setTextColor(Color.parseColor("#434A54"));
        logical_subject_optiond.setBackground(getResources().getDrawable(R.drawable.whitestyle));
        logical_subject_optiond.setTextColor(Color.parseColor("#434A54"));
        logical_subject_optiona.setPadding(25,0,0,0);
        logical_subject_optionb.setPadding(25,0,0,0);
        logical_subject_optionc.setPadding(25,0,0,0);
        logical_subject_optiond.setPadding(25,0,0,0);
    }

    public void onClickOption(View view){
        logical_subject_optiona.setBackground(getResources().getDrawable(R.drawable.whitestyle));
        logical_subject_optiona.setTextColor(Color.parseColor("#434A54"));
        logical_subject_optionb.setBackground(getResources().getDrawable(R.drawable.whitestyle));
        logical_subject_optionb.setTextColor(Color.parseColor("#434A54"));
        logical_subject_optionc.setBackground(getResources().getDrawable(R.drawable.whitestyle));
        logical_subject_optionc.setTextColor(Color.parseColor("#434A54"));
        logical_subject_optiond.setBackground(getResources().getDrawable(R.drawable.whitestyle));
        logical_subject_optiond.setTextColor(Color.parseColor("#434A54"));
         switch (view.getId()){
             case R.id.logical_subject_optiona:
                 if (selectAnswer.equals("A")){
                     selectAnswer = "";
                     logical_subject_optiona.setBackground(getResources().getDrawable(R.drawable.whitestyle));
                     logical_subject_optiona.setTextColor(Color.parseColor("#434A54"));
                 }else {
                     selectAnswer = "A";
                     logical_subject_optiona.setBackground(getResources().getDrawable(R.drawable.limitstyle));
                     logical_subject_optiona.setTextColor(Color.parseColor("#48CFAD"));
                 }
                 break;
             case R.id.logical_subject_optionb:
                 if (selectAnswer.equals("B")){
                     selectAnswer = "";
                     logical_subject_optionb.setBackground(getResources().getDrawable(R.drawable.whitestyle));
                     logical_subject_optionb.setTextColor(Color.parseColor("#434A54"));
                 }else {
                     selectAnswer = "B";
                     logical_subject_optionb.setBackground(getResources().getDrawable(R.drawable.limitstyle));
                     logical_subject_optionb.setTextColor(Color.parseColor("#48CFAD"));
                 }
                 break;
             case R.id.logical_subject_optionc:
                 if (selectAnswer.equals("C")){
                     selectAnswer = "";
                     logical_subject_optionc.setBackground(getResources().getDrawable(R.drawable.whitestyle));
                     logical_subject_optionc.setTextColor(Color.parseColor("#434A54"));
                 }else {
                     selectAnswer = "C";
                     logical_subject_optionc.setBackground(getResources().getDrawable(R.drawable.limitstyle));
                     logical_subject_optionc.setTextColor(Color.parseColor("#48CFAD"));
                 }
                 break;
             case R.id.logical_subject_optiond:
                 if (selectAnswer.equals("D")){
                     selectAnswer = "";
                     logical_subject_optiond.setBackground(getResources().getDrawable(R.drawable.whitestyle));
                     logical_subject_optiond.setTextColor(Color.parseColor("#434A54"));
                 }else {
                     selectAnswer = "D";
                     logical_subject_optiond.setBackground(getResources().getDrawable(R.drawable.limitstyle));
                     logical_subject_optiond.setTextColor(Color.parseColor("#48CFAD"));
                 }
                 break;
         }
         logical_subject_optiona.setPadding(25,0,0,0);
         logical_subject_optionb.setPadding(25,0,0,0);
         logical_subject_optionc.setPadding(25,0,0,0);
         logical_subject_optiond.setPadding(25,0,0,0);
    }

    private void getData(){
        avi.show();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        if (homeDataModel.getAppid().contains("selectappid")){
            map.put(APIKey.typeKey,homeDataModel.getType()+"");
        }
        String url = "miniapp/"+homeDataModel.getAppid()+"/getlogicalinfo.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                avi.hide();
                Log.e("TAG","data="+data+"\n");
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    logical_subject_layout.setVisibility(View.VISIBLE);
                    wordUserinfoModel = GsonTools.getWordUserinfoModel(data);
                    logicalSubjectModel = GsonTools.getLogicalSubjectModel(data);
                    goldcount = GsonTools.getLogicalGoldCount(data);
                    logical_subject_tip.setText("当前金币:"+wordUserinfoModel.getGoldcount()+"   答对:+"+goldcount+"   答错:-"+goldcount+"   查看解析:-"+goldcount*2);
                    logical_subject_title.setText("第 "+(wordUserinfoModel.getPlaylevel()+1)+" 题");
                    String content = logicalSubjectModel.getContent();
                    if(!CommonUtils.isEmpty(logicalSubjectModel.getTypestr())){
                        content = "【"+logicalSubjectModel.getTypestr()+"】"+content;
                    }
                    logical_subject_content.setText(content);

                    if (CommonUtils.isEmpty(logicalSubjectModel.getImgurl())){
                        logical_subject_img.setVisibility(View.GONE);
                        logical_subject_bg.setVisibility(View.GONE);
                    }else {
                        logical_subject_img.setVisibility(View.VISIBLE);
                        logical_subject_bg.setVisibility(View.VISIBLE);
                        Glide.with(LogicalSubjectActivity.this).load(logicalSubjectModel.getImgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(logical_subject_img);
                    }

                    logical_subject_optiona.setText(logicalSubjectModel.getOptiona());
                    logical_subject_optionb.setText(logicalSubjectModel.getOptionb());

                    if (logicalSubjectModel.getOptioncount() > 2){
                        logical_subject_optionc.setVisibility(View.VISIBLE);
                        logical_subject_optionc.setText(logicalSubjectModel.getOptionc());
                    }else {
                        logical_subject_optionc.setVisibility(View.INVISIBLE);
                    }

                    if (logicalSubjectModel.getOptioncount() > 3){
                        logical_subject_optiond.setVisibility(View.VISIBLE);
                        logical_subject_optiond.setText(logicalSubjectModel.getOptiond());
                    }else {
                        logical_subject_optiond.setVisibility(View.INVISIBLE);
                    }
                    changeToNormal();
                    Animation animation = AnimationUtils.loadAnimation(LogicalSubjectActivity.this, R.anim.pushfromleft);
                    logical_subject_layout.startAnimation(animation);

                }else{
                    logical_subject_layout.setVisibility(View.INVISIBLE);
                    ToastUtil.show(LogicalSubjectActivity.this,"亲，暂时没有更多题目了，敬请期待~");
                }
            }

            @Override
            public void onError(String msg) {
                 avi.hide();
            }
        });
    }

    private void checkAnswer(){
        if (wordUserinfoModel == null){
            return;
        }
        if (selectAnswer.length() == 0){
            ToastUtil.show(LogicalSubjectActivity.this,"亲，请先选择要提交的答案~");
            return;
        }
        if (wordUserinfoModel.getPlaylevel() >= logicalSubjectModel.getId()){
            ToastUtil.show(LogicalSubjectActivity.this,"亲，本题你已经回答正确啦~");
            return;
        }
        if (isChecking == true){
            return;
        }
        isChecking = true;
        avi.show();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        map.put(APIKey.idKey,logicalSubjectModel.getId()+"");
        map.put(APIKey.answerKey,selectAnswer);
        String url = "miniapp/"+homeDataModel.getAppid()+"/checklogicalanswer.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                avi.hide();
                isChecking = false;
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    CustomDialog dialog = null;
                    final CustomDialog.Builder builder = new CustomDialog.Builder(LogicalSubjectActivity.this);
                    builder.setTitle("本题解析");
                    builder.setMessage(logicalSubjectModel.getExlain());
                    builder.setPositiveButton("下一题", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            getData();
                        }
                    });
                    dialog = builder.create();
                    dialog.show();
                }else {
                    ToastUtil.show(LogicalSubjectActivity.this,commonModel.getMsg());
                    if (wordUserinfoModel.getGoldcount() >= goldcount){
                        wordUserinfoModel.setGoldcount(wordUserinfoModel.getGoldcount()-goldcount);
                    }
                    logical_subject_tip.setText("当前金币:"+wordUserinfoModel.getGoldcount()+"   答对:+"+goldcount+"   答错:-"+goldcount+"   查看解析:-"+goldcount*2);
                }
            }

            @Override
            public void onError(String msg) {
                isChecking = false;
                avi.hide();
            }
        });
    }

    private void getExplain(){
        if (logicalSubjectModel == null){
            return;
        }
        if (wordUserinfoModel.getPlaylevel() >= logicalSubjectModel.getId()){
            CustomDialog dialog = null;
            final CustomDialog.Builder builder = new CustomDialog.Builder(LogicalSubjectActivity.this);
            builder.setTitle("本题解析");
            builder.setMessage(logicalSubjectModel.getExlain());
            builder.setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            dialog = builder.create();
            dialog.show();
            return;
        }
        if (isGetTip == true){
            return;
        }
        isGetTip = true;
        avi.show();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        map.put(APIKey.idKey,logicalSubjectModel.getId()+"");
        String url = "miniapp/"+homeDataModel.getAppid()+"/getlogicalexplain.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                avi.hide();
                isGetTip = false;
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    wordUserinfoModel.setGoldcount(wordUserinfoModel.getGoldcount()-goldcount*2);
                    logical_subject_tip.setText("当前金币:"+wordUserinfoModel.getGoldcount()+"   答对:+"+goldcount+"   答错:-"+goldcount+"   查看解析:-"+goldcount*2);
                    CustomDialog dialog = null;
                    final CustomDialog.Builder builder = new CustomDialog.Builder(LogicalSubjectActivity.this);
                    builder.setTitle("本题解析");
                    builder.setMessage(logicalSubjectModel.getExlain());
                    builder.setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    dialog = builder.create();
                    dialog.show();
                }
            }

            @Override
            public void onError(String msg) {
                isGetTip = false;
                avi.hide();
            }
        });

    }


    private void share(View v){
        /*
        menuWindow = new PublishSelectPicPopupWindow(LogicalSubjectActivity.this,itemsOnClick,list);
        // 显示窗口
        menuWindow.showAtLocation(v,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        */
        ActionSheetDialog actionSheetDialog = new ActionSheetDialog(LogicalSubjectActivity.this)
                .builder()
                .setTitle("请选择")
                .setCancelable(false)
                .setCanceledOnTouchOutside(true);
        for (String s : list){
            actionSheetDialog.addSheetItem(s, null, new ActionSheetDialog.OnSheetItemClickListener() {
                @Override
                public void onClick(int which) {
                    if (which == 1){
                        //分享给微信好友
                        String des = "分享自微句";
                        if (logicalSubjectModel != null){
                            des =logicalSubjectModel.getContent();
                            if (des.length() > 50){
                                des = des.substring(0,50);
                            }
                        }
                        String content = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                        shareToWX(1,url,content,des);
                    }else if (which == 2){
                        //分享到朋友圈
                        String title = "【"+homeDataModel.getName()+"】";
                        if (logicalSubjectModel != null){
                            String t = logicalSubjectModel.getContent();
                            if (t.length() > 50){
                                t = t.substring(0,50);
                            }
                            title += t;
                        }
                        String c = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                        shareToWX(0,url,c,title);
                    }else if (which == 3){
                        //分享到qq
                        String title = "【"+homeDataModel.getName()+"】";
                        if (logicalSubjectModel != null){
                            String t = logicalSubjectModel.getContent();
                            if (t.length() > 50){
                                t = t.substring(0,50);
                            }
                            title += t;
                        }
                        String c = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                        shareToQQ(0,url,c,title);
                    }else if (which == 4){
                        //qq空间
                        String title = "【"+homeDataModel.getName()+"】";
                        if (logicalSubjectModel != null){
                            String t = logicalSubjectModel.getContent();
                            if (t.length() > 50){
                                t = t.substring(0,50);
                            }
                            title += t;
                        }
                        String c = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                        shareToQQ(1,url,c,title);
                    }
                }
            });
        }
        actionSheetDialog.show();
    }

    //分享到qq
    private Bundle params;
    private void shareToQQ(int type, String url,String title,String content){
        params = new Bundle();
        if (type == 0){//分享给QQ好友
            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
            params.putString(QQShare.SHARE_TO_QQ_TITLE, title);// 标题
            params.putString(QQShare.SHARE_TO_QQ_SUMMARY, content);// 摘要
            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,url);// 内容地址
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"http://image.daydayrise.net/360@2x.png");// 网络图片地址
            params.putString(QQShare.SHARE_TO_QQ_EXT_INT, "");//其它附加功能
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    CustomApplication.mTencent.shareToQQ(LogicalSubjectActivity.this, params, mIUiListener);
                }
            });
        }else {
            //分享到QQ空间
            params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
            params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);// 标题
            params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, content);// 摘要
            params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL,url);// 内容地址
            ArrayList<String> imgUrlList = new ArrayList<>();
            imgUrlList.add("http://image.daydayrise.net/360@2x.png");
            params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imgUrlList);// 图片地址
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    CustomApplication.mTencent.shareToQzone(LogicalSubjectActivity.this, params, mIUiListener);
                }
            });
        }

    }

    // 为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.first_button://分享给微信好友
                    String des = "分享自微句";
                    if (logicalSubjectModel != null){
                        des =logicalSubjectModel.getContent();
                        if (des.length() > 50){
                            des = des.substring(0,50);
                        }
                    }
                    String content = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                    shareToWX(1,url,content,des);
                    break;
                case R.id.second_button://分享到微信朋友圈
                    String title = "【"+homeDataModel.getName()+"】";
                    if (logicalSubjectModel != null){
                        String t = logicalSubjectModel.getContent();
                        if (t.length() > 50){
                            t = t.substring(0,50);
                        }
                        title += t;
                    }
                    String c = homeDataModel.getName()+"，好玩又益智，赶紧来体验吧~";
                    shareToWX(0,url,title,c);
                    break;
                case R.id.third_button:
                    break;
                default:
                    break;
            }

        }
    };

    //分享到微信
    private void shareToWX(int type,String url,String title,String content){
        //初始化一个WXWebpageObject对象，填写url
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        //用WXWebpageObject对象初始化一个WXMediaMessage对象，填写标题、描述
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = content;
        Bitmap thumb = BitmapFactory.decodeResource(getResources(),R.mipmap.icon);
        msg.thumbData = CommonUtils.bitmapToByteArray(thumb,true);
        //构成一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = msg;
        req.scene = type==0 ? SendMessageToWX.Req.WXSceneTimeline:SendMessageToWX.Req.WXSceneSession;
        CustomApplication.mWxApi.sendReq(req);

    }

    private void getShareGold(){
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        String url = "miniapp/"+wordUserinfoModel.getAppid()+"/usershare.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                final CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    wordUserinfoModel = GsonTools.getWordUserinfoModel(data);
                    new Handler().postDelayed(new Runnable(){
                        public void run() {
                            ToastUtil.show(LogicalSubjectActivity.this,commonModel.getMsg());
                            logical_subject_tip.setText("当前金币:"+wordUserinfoModel.getGoldcount()+"   答对:+"+goldcount+"   答错:-"+goldcount+"   查看解析:-"+goldcount*2);
                        }
                    }, 10);
                }
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    /**
     * 广播接收者
     * @author Gordon
     */
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BroadcastDefine.WX_Share_Result)) {
                //微信分享成功
                getShareGold();
            }
        }
    };

    /**
     * 注册广播
     * @author Gordon
     */
    public void registerBoradcastReceiver(){
        IntentFilter myIntentFilter = new IntentFilter();
        //添加接收广播类型
        myIntentFilter.addAction(BroadcastDefine.WX_Share_Result);
        //注册广播
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    private void signDo(){
        if (wordUserinfoModel == null){
            return;
        }
        avi.show();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
        String url = "miniapp/"+wordUserinfoModel.getAppid()+"/usersign.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
            @Override
            public void onSuccess(String data) {
                avi.hide();
                Log.e("TAG",data);
                final CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    wordUserinfoModel = GsonTools.getWordUserinfoModel(data);
                    new Handler().postDelayed(new Runnable(){
                        public void run() {
                            ToastUtil.show(LogicalSubjectActivity.this,commonModel.getMsg());
                            logical_subject_tip.setText("当前金币:"+wordUserinfoModel.getGoldcount()+"   答对:+"+goldcount+"   答错:-"+goldcount+"   查看解析:-"+goldcount*2);
                        }
                    }, 10);
                }else {
                    ToastUtil.show(LogicalSubjectActivity.this,"你已领取");
                }
            }
            @Override
            public void onError(String msg) {
                avi.hide();
            }
        });
    }



}
